package base

import (
	"JVM/Jvmgo/GoToJVM/runtimeData"
	"JVM/Jvmgo/GoToJVM/runtimeData/heap"
)

func InvokeMethod(invokerFrame *runtimeData.Frame, method *heap.Method) {
	// 创建新帧，推入虚拟机栈
	thread := invokerFrame.Thread()
	newFrame := thread.NewFrame(method)
	thread.PushFrame(newFrame)

	// 传递参数（double，long占用两个位置，实例方法Java编译器会在前面加上一个this参数）
	argSlotCount := int(method.ArgSlotCount())
	if argSlotCount > 0 {
		for i := argSlotCount - 1; i >= 0; i-- {
			slot := invokerFrame.OperandStack().PopSlot()
			// 通过操作 Slot 结构体来屏蔽底层字节差距
			newFrame.LocalVars().SetSlot(uint(i), slot)
		}
	}
}
